﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

public partial class Admin_Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateControls();
            InitDDLCategory();
        }
    }

    private void PopulateControls()
    {
        int catID = Convert.ToInt32(Request.QueryString["CatID"] ?? "0");
        string name = Request.QueryString["Name"] ?? "";
        txtName.Text = name;
        ddlCategory.SelectedValue = catID.ToString();

        string orderCol = Request.QueryString["Order"] ?? "Name";
        string type = Request.QueryString["Type"] ?? "ASC";

        int howManyPages = 0;
        string page = Request.QueryString["Page"] ?? "1";
        string firstPageUrl = Link.ToAdminProduct("1&Name=" + name + "&CatID=" + catID + "&Order=" + orderCol + "&Type=" + type);
        string pagerUrl = Link.ToAdminProduct("{0}&Name=" + name + "&CatID=" + catID + "&Order=" + orderCol + "&Type=" + type);

        List<Product> ds = Product.Filter(catID, name, orderCol, type, page, out howManyPages);

        repProd.DataSource = ds;
        repProd.DataBind();

        pagerTop.Show(int.Parse(page), howManyPages, firstPageUrl, pagerUrl, false);
        pagerBottom.Show(int.Parse(page), howManyPages, firstPageUrl, pagerUrl, true);
    }

    private void InitDDLCategory()
    {
        List<Category> c = Category.All();
        ddlCategory.DataSource = c;
        ddlCategory.DataTextField = "Name";
        ddlCategory.DataValueField = "CategoryID";
        ddlCategory.DataBind();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        btnDelete.Click += new EventHandler(btnDelete_Click);
        repProd.ItemCommand += new RepeaterCommandEventHandler(repProd_ItemCommand);
    }

    void repProd_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "UpdatePromo")
        {
            // CommandArgument co dang ProductID_PromoFront 
            string[] args = e.CommandArgument.ToString().Split('_');
            // args[0] ==> ProductID 
            // args[1] ==> PromoFront (kieu String) 
            Product.UpdatePromo(args[0], !Boolean.Parse(args[1]));
            PopulateControls();
        }
        else
            if (e.CommandName == "SortName")
                Sort("Name");
            else
                if (e.CommandName == "SortPrice")
                    Sort("Price");
                else
                    if (e.CommandName == "SortPromo")
                        Sort("Promo");
                    else
                        if (e.CommandName == "SortID")
                            Sort("ID");
    }

    void btnDelete_Click(object sender, EventArgs e)
    {
        string stringid = Request.Form["cid"] ?? "";
        if (stringid != "")
        {
            foreach (string id in stringid.Split(','))
                Product.Delete(id);
            PopulateControls();
        }
    }

    protected void btnFilter_Click(object sender, EventArgs e)
    {
        int catID = Convert.ToInt32(ddlCategory.SelectedValue);
        string name = "";
        if (!String.IsNullOrWhiteSpace(txtName.Text))
            name = txtName.Text;
        Response.Redirect("Product.aspx?Name=" + name + "&CatID=" + catID);
    }

    void Sort(string orderCol)
    {
        string type = Request.QueryString["Type"] ?? "ASC";
        type = type == "ASC" ? "DESC" : "ASC";
        int catID = Convert.ToInt32(Request.QueryString["CatID"] ?? "0");
        string name = Request.QueryString["Name"] ?? "";
        Response.Redirect("Product.aspx?Name=" + name + "&CatID=" + catID + "&Order=" + orderCol + "&Type=" + type);
    }
}
